<HTML><HEAD>
<!--
    --------------------------
    Strings to Arrays and Back
    --------------------------
-->

<SCRIPT LANGUAGE="JavaScript"><!-- hide from old browsers

/*
    THE JAVASCRIPT COOKBOOK by Erica Sadun, webrx@mindspring.com
    Copyright (c)2000 by Charles River Media.  All Rights Reserved.
    
    This applet can only be re-used or modifed by license holders of the
    JavaScript Cookbook CD-ROM.  Credit must be given in the source
    code and this copyright notice must be maintained. If you do
    not hold a license to the JavaScript Cookbook, you may NOT
    duplicate or modify this code for your own use.

    Use at your own risk. No warranty is given or implied of the suitability 
    of this applet for any specific application. Neither Erica Sadun nor 
    Charles River Media will be held responsible for any unwanted effects 
    due to the use of this applet or any derivative. 
*/

// Create an Array
function createArray(n)
{

    for (var i = 0; i < n; i++)
		this[i] = 0;
    return this;
}

// String to Array
function stringArray(aString)
{
	var t = new Array();
    var s = ""+aString
    
    if (s == ""){
        return t;
    }
    var i = 0
    var idx = 0
    
    // search for the colon to create each array item
    while ((idx = s.indexOf(':')) != -1)
    {
        t[i] = s.substring(0, idx)
        s = s.substring(idx+1,s.length)
        i++
    }
    
    // create final array item
    t[i]=s;
    
    return t;
}
    
// Array to String
function arrayString(anArray, len)
{
    var s = ""
    
    for (var i = 0; i < (len - 1); i++) s += anArray[i]+":"
    if (len > 0) s += anArray[len - 1]
    return s
}

// Initialize Global Variables
var myArray = new createArray(50) // 50 should be big enough
var arrayTop = 0;              // Top of myArray

// Show the Array
function showArray()
{
    document.forms[0].myArray.value = arrayString(myArray, arrayTop)
    
    // prepare for next entry
    document.forms[0].input.focus()
    document.forms[0].input.select()
}

// Add value to array
function add()
{
    myArray[arrayTop]=""+document.forms[0].input.value
    arrayTop++; // increase top
    showArray()
}

// Clear the array
function clearArray()
{
    arrayTop = 0;
    document.forms[0].input.value=''
	document.forms[0].myArray.value=''
	for(var i = 0; i < myArray.length; i++)
		myArray[i] = '';
    showArray()
}

// Enter a string
function enter()
{    
    var s = ""+document.forms[0].myArray.value;
    var ra = stringArray(s);
    arrayTop = ra.length;
    for (var i = 0; i < arrayTop; i++) myArray[i] = ra[i];
    showArray();
    return true;
}
    
// Sort the array using a simple bubblesort
function sort()
{
    for (var i = 0; i < arrayTop; i++)
    {
        for (var j = i; j < arrayTop; j++)
            if (myArray[i] > myArray[j])
            {
                // out of order: swap them.
                var tmp = myArray[i]
                myArray[i] = myArray[j]
                myArray[j] = tmp
            }
    }
    showArray()
}
<!-- done hiding --></SCRIPT></HEAD>

<BODY bgcolor="ffffff" link="0000ff" vlink="770077"
    onLoad="document.forms[0].input.focus();document.forms[0].input.select()">

<FONT COLOR="007777"><H1><IMG SRC="../GRAFX/UTENS.JPG" WIDTH=80 HEIGHT=50
    ALIGN = CENTER>Strings to Arrays and Back</H1></FONT>

<FONT COLOR="770000">
    This applet demonstrates how array data may be stored to
    and recovered from strings. For example: <ul>
    <li>Type 'f' and tap [Add to Array];
    <li>Type 'a' and tap [Add to Array];
    <li>Tap on the top text entry field to change focus;
    <li>Add in ':c:d:g:e:b';
    <li>Tap [Sort Array].
    </ul>
</FONT></BLOCKQUOTE>

<BR><BR>

</SCRIPT><CENTER><FORM><TABLE BORDER=1>

<TR>
<TD align=center  colspan=3><input type="text" name="myArray" value="" 
    size=40 onBlur="enter()">String</TD>
</TR>

<TR>
<TD align=center  colspan=3><input type="text" name="input" 
value="" size=8></TD>
</TR>

<TR>
<TD align=center>
<input type="button" value="Sort Array" onClick="sort()"></TD>
<TD align=center>
<input type="button" value="Add to Array" onClick="add()"></TD>
<TD align=center>
<input type="button" value="Clear Array" onClick="clearArray()"></TD>
</TR> 

</TABLE></FORM></CENTER>

<br><br>
<FONT COLOR="007777"><H2>Discussion</H2></FONT>
<FONT SIZE=4>
    Tapping on [Add to Array] directly adds data to the array data
    structure.  This structure is converted to a string and shown
    in the text box labeled "String".  This field, too, may
    be edited.  When it senses a field "blur" event, it transforms
    its string into an array. This array can be sorted, transformed
    back to a string, and displayed.<p>
    This method of converting arrays to strings and back will be
    particularly useful to PERL programmers.
</FONT>

<FONT COLOR="770000"><PRE>
// String to Array
function stringArray(aString)
{
    var i = 0
    var idx = 0
    var s = ""+aString
    
    if (s == "")
    {
//      this.length = 0
        return this
    }
    
    // search for the colon to create each array item
    while ((idx = s.indexOf(':')) != -1)
    {
        this[i] = s.substring(0, idx)
        s = s.substring(idx+1,s.length)
        i++
    }
    
    // create final array item
    this[i]=s
//  this.length = i+1
    
    return this
}
    
// Array to String
function arrayString(anArray, len)
{
    var s = ""
    
    for (var i = 0; i < (len - 1); i++) s += anArray[i]+":"
    if (len > 0) s += anArray[len - 1]
    return s
}
</PRE></FONT>

<h5>Copyright ©2000 by Charles River Media, All Rights Reserved</h5>
</BODY>
</HTML>